package com.zhhs.galaxyott.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.zhhs.galaxyott.Constant;
import com.zhhs.galaxyott.MyApplication;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.DeviceCenterActivity;
import com.zhhs.galaxyott.beans.VipDataBean;
import com.zhhs.galaxyott.http.api.RequestMaker;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.response.VipBean;
import com.zhhs.galaxyott.httpVolley.response.VipDataResponse;
import com.zhhs.galaxyott.sharepreference.SharedPrefHelper;
import com.zhhs.galaxyott.utils.Logs;
import com.zhhs.galaxyott.utils.PurchaseOrPayUtils;

public class PersonalFragment extends BaseFragment implements OnClickListener {

    private static String TAG = PersonalFragment.class.getSimpleName();

    private static PersonalFragment f;
    private static View view;

    private TextView ca_number, vip_pay_first_text, vip_pay_second_text, vip_pay_three_text;
    private LinearLayout vip_layout;
    private static FrameLayout vip_pay_first, vip_pay_second, vip_pay_three;

    private static LinearLayout pay_layout;

    private SharedPrefHelper sp;

    List<VipDataBean> mlvdb = new ArrayList<VipDataBean>();

    private VipBean mvipBean;

    public static PersonalFragment newInstance() {
        if (f == null) {

            f = new PersonalFragment();
        }
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sp = SharedPrefHelper.getInstance(getActivity());
        mvipBean = MyApplication.getVipBean();

    }

    private void loadVipData() {
        RequestMaker.getVipData(new ResponseListener<VipDataResponse>() {

            @Override
            public void onResponse(VipDataResponse result) {

                if (result == null || result.lvdb.size() == 0) {
                    return;
                }
                Logs.logD("DEBUG_DT", "THIS IS VipData-->" + result.errCode);
                if (result.errCode == 0) {
                    mlvdb = result.lvdb;
                    if (mlvdb.size() > 0) {
                        vip_pay_first_text.setText(mlvdb.get(0).getPackageName() + "  "
                                + mlvdb.get(0).getPackagePrice() + "元");
                    }
                    if (mlvdb.size() > 1) {
                        vip_pay_second_text.setText(mlvdb.get(1).getPackageName() + "  "
                                + mlvdb.get(1).getPackagePrice() + "元");
                    }
                    if (mlvdb.size() > 2) {
                        vip_pay_three_text.setText(mlvdb.get(2).getPackageName() + "  "
                                + mlvdb.get(2).getPackagePrice() + "元");
                    }

                }
            }

            @Override
            public void onError(VolleyError error) {
                Logs.logD("DEBUG_DT", "THIS IS VipData-->-1");

            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_personcenter, null);

        ca_number = (TextView) view.findViewById(R.id.ca_number);
        ca_number.setText("CA卡号:" + sp.getsmartcard());
        vip_layout = (LinearLayout) view.findViewById(R.id.vip_layout);

        pay_layout = (LinearLayout) view.findViewById(R.id.pay_layout);
        vip_pay_first = (FrameLayout) view.findViewById(R.id.vip_pay_first);
        vip_pay_second = (FrameLayout) view.findViewById(R.id.vip_pay_second);
        vip_pay_three = (FrameLayout) view.findViewById(R.id.vip_pay_three);

        vip_pay_first_text = (TextView) view.findViewById(R.id.vip_pay_first_text);
        vip_pay_second_text = (TextView) view.findViewById(R.id.vip_pay_second_text);
        vip_pay_three_text = (TextView) view.findViewById(R.id.vip_pay_three_text);

        if (sp.getIsVip()) {
            vip_layout.setVisibility(View.VISIBLE);
            vip_pay_first_text.setText(mvipBean.getPackageName());
            vip_pay_second_text.setText(mvipBean.getPackagePrice() + "元");
            vip_pay_first_text.setText("有效期至" + mvipBean.getExpiryDate());
        } else {
            loadVipData();
            vip_layout.setVisibility(View.GONE);
        }

        vip_pay_first.setOnClickListener(this);
        vip_pay_second.setOnClickListener(this);
        vip_pay_three.setOnClickListener(this);

        this.view = view;
        return view;
    }

    public static boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (view.findFocus() == null) {
                vip_pay_first.setFocusable(true);
                vip_pay_first.requestFocus();

                DeviceCenterActivity.detailFocus = true;
            } else {
                vip_pay_second.setFocusable(true);
                vip_pay_three.setFocusable(true);
            }

            break;

        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (view.findFocus() != null && (view.findFocus().getId() == R.id.vip_pay_first)) {
                DeviceCenterActivity.detailFocus = false;
                vip_pay_first.setFocusable(false);
                vip_pay_second.setFocusable(false);
                vip_pay_three.setFocusable(false);
            }

            break;

        case KeyEvent.KEYCODE_DPAD_DOWN:

        default:
            break;
        }
        return false;

    }

    @Override
    public void onClick(View v) {

        if (sp.getIsVip()) {
            return;
        }

        switch (v.getId()) {
        case R.id.vip_pay_first:

            if (mlvdb.get(0).getPackagePrice() != null) {
                Constant.contentId = mlvdb.get(0).getPackageId();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        PurchaseOrPayUtils.getComboValid(getActivity(), mlvdb.get(0).getPackageCode(), mlvdb.get(0)
                                .getPackagePrice());
                    }
                }).start();

            }

            break;
        case R.id.vip_pay_second:

            if (mlvdb.get(1).getPackagePrice() != null) {
                Constant.contentId = mlvdb.get(1).getPackageId();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        PurchaseOrPayUtils.getComboValid(getActivity(), mlvdb.get(1).getPackageCode(), mlvdb.get(1)
                                .getPackagePrice());
                    }
                }).start();
            }
            break;
        case R.id.vip_pay_three:
            if (mlvdb.get(2).getPackagePrice() != null) {
                Constant.contentId = mlvdb.get(2).getPackageId();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        PurchaseOrPayUtils.getComboValid(getActivity(), mlvdb.get(2).getPackageCode(), mlvdb.get(2)
                                .getPackagePrice());
                    }
                }).start();
            }
            break;

        default:
            break;
        }
    }
    // private void payJump() {
    // ComponentName component = new ComponentName("com.crunii.ccn.tvhall",
    // "com.crunii.ccn.tvhall.activity.PayActivity");
    // Intent intent = new Intent();
    // intent.setComponent(component);
    // intent.putExtra("payurl", "http://192.168.5.74:9000/ccn-pay/payPlatform"
    // +
    // "?payChannel=ccn4b411a7a76bcb35f&userName=王小洋&orderId=test20160518164721828173821"
    // +
    // "&payAmount=1&targetId=10208880001020193&cardNo=9950000002100169&extend=充值缴费"
    // + "&notifyUrl= http://192.168.5.73/ccn-tv/cz/notify" +
    // "&homeUrl=ccntvhall://platformapi/home"
    // + "&returnUrl=ccntvhall://platformapi/return" +
    // "&rechargeType=1&rechargeAccount=0");
    //
    // this.startActivity(intent);
    // }

}
